<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">

    
      <link rel="icon" href="/favicon.png" />
    

    <title>
        
          CODE&#39;NOTE
        
    </title>

    <!-- Spectre.css framework -->
    <link href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/spectre.css/0.5.9/spectre.min.css" rel="stylesheet">
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/spectre.css/0.5.9/spectre-exp.min.css" rel="stylesheet">
    <link href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/spectre.css/0.5.9/spectre-icons.min.css" rel="stylesheet">

    <!-- theme css & js -->
    
<link rel="stylesheet" href="/css/book.css">

    
<script src="/js/book.js"></script>


    <!-- tocbot -->
    <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/tocbot/4.18.2/tocbot.min.js"></script>
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/tocbot/4.18.2/tocbot.css" rel="stylesheet">
    
    <!-- katex -->    
    <link href="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/KaTeX/0.15.2/katex.min.css" rel="stylesheet">

    
    
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/zooming/2.1.1/zooming.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    const zooming = new Zooming()
    zooming.listen('.book-content img')
})
</script>

<meta name="generator" content="Hexo 6.1.0"></head>

<body>

<div class="book-container">
  <div class="book-sidebar">
    <div class="book-brand">
  <a href="/">
    <img src="/favicon.png">
    <span>CODE&#39;NOTE</span>
  </a>
</div>
    <div id="menu" class="book-menu hide">
  <h2 id="☘️-Home">☘️ <a href="/">Home</a></h2>
<h2 id="Go">Go</h2>
<ul>
<li><a href="/go/01%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA">0.1 开发环境搭建</a></li>
<li><a href="/go/02%E5%8F%98%E9%87%8F%E5%92%8C%E5%B8%B8%E9%87%8F">0.2 变量和常量</a></li>
<li><a href="/go/03%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B">0.3 基本数据类型</a></li>
<li><a href="/go/04%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6">0.4 流程控制和运算符</a></li>
<li><a href="/go/05%E6%95%B0%E7%BB%84">0.5 数组</a></li>
<li><a href="/go/06%E5%88%87%E7%89%87">0.6 切片</a></li>
<li><a href="/go/07map">0.7 map</a></li>
<li><a href="/go/08%E5%87%BD%E6%95%B0">0.8 函数</a></li>
<li><a href="/go/09%E6%8C%87%E9%92%88">0.9 指针</a></li>
<li><a href="/go/10%E5%8F%8D%E5%B0%84">1.0 反射</a></li>
<li><a href="/go/11%E7%BB%93%E6%9E%84%E4%BD%93">1.1 结构体</a></li>
<li><a href="/go/12%E6%8E%A5%E5%8F%A3">1.2 接口</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/goroutine">1.3 goroutine</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/channel">1.4 channel</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%89%E5%85%A8%E5%92%8C%E9%94%81">1.5 并发安全和锁</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/%E5%8E%9F%E5%AD%90%E6%93%8D%E4%BD%9C">1.6 原子操作</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/GPM%E5%8E%9F%E7%90%86%E4%B8%8E%E8%B0%83%E5%BA%A6">1.7 GPM与调度分析</a></li>
<li><a href="/go/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/CSP%E5%B9%B6%E5%8F%91%E6%A8%A1%E5%9E%8B">1.8 CSP并发模型</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/fmt">标准库fmt</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/flag">标准库flag</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/time">标准库time</a></li>
<li><a href="/go/%E6%A0%87%E5%87%86%E5%BA%93/go_%E6%A0%87%E5%87%86%E5%BA%93log">标准库log</a></li>
</ul>
<!-- ##
## .NET

* [C# 入门基础合集](/dotnet/基础合集)
* [C# 面向对象](/dotnet/面向对象)
* [C# 数据结构](/dotnet/数据结构)
* [C# 泛型技术](/dotnet/泛型技术)
* [C# 反射技术](/dotnet/反射技术)
* [C# 多线程开发](/dotnet/多线程开发)
* [C# 多线程之线程同步技术](/dotnet/多线程之线程同步技术)
* [C# 委托和事件详解](/dotnet/委托和事件详解)
* [C# 并行编程](/dotnet/并行编程)
* [C# 异步编程](/dotnet/异步编程)
* [C# 值类型和引用类型内存分配](/dotnet/值类型和引用类型的内存分配)
* [1.1 DataProtection简介](/dotnet/1.1DataProtection简介)
* [1.2 DataProtection方法介绍](/dotnet/1.2DataProtection方法介绍)
* [1.3 落地实践及多环境调试](/dotnet/1.3落地实践及多环境调试)
* [1.4 UserSecrets](/dotnet/1.4UserSecrets)
* [1.5 数据保护方案](/dotnet/1.5基于DataProtection的数据保护方案)
* [1.6 集成AzureDevops](/dotnet/1.6方案实践及集成AzureDevops管道)


ELK Stack

* [ElastaticSearch入门](/elk/1.1ElastaticSearch入门)
* [ElastaticSearch配置](/elk/1.2ElastaticSearch配置)
* [使用Kibana操作ES](/elk/1.3使用Kibana操作ES)
 ## Docker

* [1.1 Docker概念及安装](/docker/1.1Docker概念及安装)
* [1.2 Docker镜像](/docker/1.2Docker镜像)
* [1.3 Docker容器](/docker/1.3Docker容器)
* [1.4 Docker仓库](/docker/1.4Docker仓库)
* [1.5 Docker数据管理](/docker/1.5Docker数据管理)
* [1.6 Docker容器四种网络模式](/docker/1.6Docker容器四种网络模式)
* [1.7 Docker高级网络配置](/docker/1.7Docker高级网络配置)
* [1.8 Dockerfile](/docker/1.8Dockerfile)
* [1.9 DockerCompose](/docker/1.9Docker三剑客之DockerCompose)
* [2.0 DockerMachine](/docker/2.0Docker三剑客之DockerMachine)
* [2.1 DockerSwarm](/docker/2.1Docker三剑客之DockerSwarm)
* [2.2 Docker常用命令](/docker/2.2Docker常用命令)
* [Portainer可视化面板](/docker/Portainer可视化面板)
* [1.6 Docker四种网络模式](/docker/1.6Docker容器网络模式)
* [1.7 Docker高级网络配置](/docker/1.7Docker高级网络配置)
* [1.8 Dockerfile](/docker/1.8Dockerfile)
* [1.9 DockerCompose](/docker/1.9DockerCompose)
* [2.0 DockerMachine](/docker/2.0DockerMachine)
* [2.1 DockerSwarm](/docker/2.1DockerSwarm)
* [2.2 Docker常用命令](/docker/2.2Docker常用命令)
* [Portainer可视化面板](/docker/Portainer可视化面板) 

## Linux

* [Centos安装](/linux/Centos安装)
* [Linux目录说明](/linux/Linux目录说明)
* [Centos网络配置](/linux/Centos网络配置)
* [Centos7升级gcc版本](/linux/Centos7升级gcc版本)
* [Linux常用命令](/linux/Linux常用命令)

## RabbitMQ

* [1.1 RabbitMQ概念及安装](/rabbitMq/1.1RabbitMQ概念及安装)
* [1.2 工作模式介绍](/rabbitMq/1.2工作模式介绍)
* [1.3 消息确认及持久化](/rabbitMq/1.3消息确认及持久化)
* [1.4 两种消费模式和QOS](/rabbitMq/1.4两种消费模式和QOS的实现)
* [1.5 Channel常见方法](/rabbitMq/1.5Channel常见方法)
* [1.6 RabbitMQ常用命令](/rabbitMq/1.6RabbitMQ常用命令)
* [1.7 RabbitMQ常见策略](/rabbitMq/1.7RabbitMQ常见策略)
* [1.8 RabbitMQ常见问题](/rabbitMq/1.8RabbitMQ常见问题)
* [1.9 RabbitMQ集群方案](/rabbitMq/1.9RabbitMQ集群方案)
* [客户端连接RabbitMQ](/rabbitMq/客户端连接RabbitMQ)

## Redis

* [1.1 NoSql概述](/redis/1.1NoSql概述)
* [1.2 Redis安装](/redis/1.2Redis安装)
* [1.3 Redis基本数据类型](/redis/1.3Redis基本数据类型)
* [1.4 Redis特殊数据类型](/redis/1.4Redis特殊数据类型)
* [1.5 Redis事务操作](/redis/1.5Redis事务操作)
* [1.6 Redis配置文件详解](/redis/1.6Redis配置文件详解)
* [1.7 Redis持久化](/redis/1.7Redis持久化)
* [1.8 Redis发布订阅](/redis/1.8Redis发布订阅)
* [1.9 Redis集群方案](/redis/1.9Redis集群方案)
* [1.10 Redis常见问题](/redis/1.10Redis常见问题)
* [客户端连接Redis](/redis/客户端连接Redis)
* [使用Docker搭建Redis集群](/redis/使用Docker搭建Redis集群)

## MicroService

* [1.1 微服务之项目搭建](/microservice/1.1微服务入门之项目搭建)
* [1.2 微服务之服务注册发现](/microservice/1.2微服务入门之服务注册与发现)
* [1.3 微服务之网关](/microservice/1.3微服务入门之网关)
* [1.4 微服务之事件总线](/microservice/1.4微服务入门之事件总线)
* [1.5 微服务之DockerCompose](/microservice/1.5微服务入门之DockerCompose)
* [Consul服务注册发现](/microservice/Consul服务注册发现)

## Test

* [单元测试](/test/单元测试)
* [集成测试](/test/集成测试)
* [压力测试k6](/test/压力测试k6)
* [自动化UI测试](/test/自动化UI测试) -->

</div>


<script src="/js/book-menu.js"></script>

  </div>

  <div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
  <div class="sidebar-toggle-inner"></div>
</div>

<script>
function add_inner() {
  let inner = document.querySelector('.sidebar-toggle-inner')
  inner.classList.add('show')  
}

function remove_inner() {
  let inner = document.querySelector('.sidebar-toggle-inner')
  inner.classList.remove('show')
}

function sidebar_toggle() {
    let sidebar_toggle = document.querySelector('.sidebar-toggle')
    let sidebar = document.querySelector('.book-sidebar')
    let content = document.querySelector('.off-canvas-content')
    if (sidebar_toggle.classList.contains('extend')) { // show
        sidebar_toggle.classList.remove('extend')
        sidebar.classList.remove('hide')
        content.classList.remove('extend')
    }
    else { // hide
        sidebar_toggle.classList.add('extend')
        sidebar.classList.add('hide')
        content.classList.add('extend')
    }
}
</script>

  <div class="off-canvas-content">
    <div class="columns">
      <div class="column col-10 col-lg-12">
        <div class="book-navbar">
          <!-- For Responsive Layout -->

<header class="navbar">
  <section class="navbar-section">
    <a onclick="open_sidebar()">
      <i class="icon icon-menu"></i>
    </a>
  </section>
</header>

        </div>
        <div class="book-content">
          <div class="book-post">
  <h1 id="切片定义">切片定义</h1>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">sliceDeclar</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> s1 []<span class="type">int</span>    <span class="comment">// 定义存放int类型的切片</span></span><br><span class="line">	<span class="keyword">var</span> s2 []<span class="type">string</span> <span class="comment">// 定义存放string类型的切片</span></span><br><span class="line">	fmt.Println(s1, s2)</span><br><span class="line">	fmt.Println(s1 == <span class="literal">nil</span>, s2 == <span class="literal">nil</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>创建切片和创建数组非常相似，如果在 <code>[]</code> 指定了值，那么创建的是一个数组，反之就是一个切片</p>
<h1 id="创建空切片">创建空切片</h1>
<p>空切片在底层数组包含 0 个元素，也没有分配任何存储空间。一般用于表示空集合。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">createEmptySlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	slice1 := []<span class="type">int</span>&#123;&#125;</span><br><span class="line">	slice2 := <span class="built_in">make</span>([]<span class="type">int</span>, <span class="number">0</span>)</span><br><span class="line">	fmt.Println(slice1, slice2)</span><br><span class="line">	fmt.Println(slice1 == <span class="literal">nil</span>, slice2 == <span class="literal">nil</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="基于数组得到切片">基于数组得到切片</h1>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">createSliceByArray</span><span class="params">()</span></span> &#123;</span><br><span class="line">	arr := [...]<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;</span><br><span class="line">	fmt.Println(arr)</span><br><span class="line">	fmt.Println(arr[<span class="number">0</span>:<span class="number">4</span>]) <span class="comment">// =&gt;[1 2 3 4] 基于数组得到切片,从0开始到第4个结束（不包含4）.原则：左包含右不包含</span></span><br><span class="line">	fmt.Println(arr[:<span class="number">4</span>])  <span class="comment">// =&gt;[1 2 3 4] 省略第一个参数，默认从0开始</span></span><br><span class="line">	fmt.Println(arr[<span class="number">3</span>:])  <span class="comment">// =&gt;[4 5 6 7 8 9] 省略第二个参数，默认到len(a1)结束</span></span><br><span class="line">	fmt.Println(arr[:])   <span class="comment">// =&gt;[1 2 3 4 5 6 7 8 9] 两个参数都省略，默认从0开始到len(a1-1)结束</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[1 2 3 4 5 6 7 8 9]</span><br><span class="line">[1 2 3 4]</span><br><span class="line">[1 2 3 4]</span><br><span class="line">[4 5 6 7 8 9]      </span><br><span class="line">[1 2 3 4 5 6 7 8 9]</span><br></pre></td></tr></table></figure>
<h1 id="基于切片得到切片">基于切片得到切片</h1>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">createSliceBySlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	arr := [...]<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;</span><br><span class="line">	arr1 := arr[<span class="number">0</span>:<span class="number">4</span>]</span><br><span class="line">	fmt.Println(arr1)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(s5):%d cap(s5):%d \n&quot;</span>, <span class="built_in">len</span>(arr1), <span class="built_in">cap</span>(arr1))</span><br><span class="line">	<span class="comment">//由切片得到切片</span></span><br><span class="line">	arr2 := arr1[<span class="number">2</span>:<span class="number">4</span>]</span><br><span class="line">	fmt.Println(arr2)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(s5):%d cap(s5):%d \n&quot;</span>, <span class="built_in">len</span>(arr2), <span class="built_in">cap</span>(arr2))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[1 2 3 4]</span><br><span class="line">len(s5):4 cap(s5):9 </span><br><span class="line">[3 4]</span><br><span class="line">len(s5):2 cap(s5):7 </span><br></pre></td></tr></table></figure>
<h1 id="直接创建并初始化">直接创建并初始化</h1>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">createSlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	s1 := []<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">67</span>, <span class="number">88</span>&#125;</span><br><span class="line">	s2 := []<span class="type">string</span>&#123;<span class="string">&quot;北京&quot;</span>, <span class="string">&quot;上海&quot;</span>, <span class="string">&quot;山西&quot;</span>&#125;</span><br><span class="line">	fmt.Println(s1, s2)</span><br><span class="line">	fmt.Println(s1 == <span class="literal">nil</span>, s2 == <span class="literal">nil</span>)</span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(s1):%d cap(s1):%d \n&quot;</span>, <span class="built_in">len</span>(s1), <span class="built_in">cap</span>(s1))</span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(s2):%d cap(s2):%d \n&quot;</span>, <span class="built_in">len</span>(s2), <span class="built_in">cap</span>(s2))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="使用-make-创建切片">使用 make 创建切片</h1>
<p>以上大部分都是基于数组来创建切片，如果需要动态的创建一个切片，可以使用内置的<code>make()</code>函数，格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make([]T, size, <span class="built_in">cap</span>)</span><br></pre></td></tr></table></figure>
<p>其中：</p>
<ul>
<li><code>T</code>:切片的元素类型</li>
<li><code>size</code>:切片中元素的数量</li>
<li><code>cap</code>:切片的容量</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">createSliceByMake</span><span class="params">()</span></span> &#123;</span><br><span class="line">	slice1 := <span class="built_in">make</span>([]<span class="type">string</span>, <span class="number">5</span>)</span><br><span class="line">	<span class="comment">// 使用make创建一个长度5，容量为10的切片</span></span><br><span class="line">	slice2 := <span class="built_in">make</span>([]<span class="type">string</span>, <span class="number">5</span>, <span class="number">10</span>)</span><br><span class="line">	fmt.Println(slice1, slice2)</span><br><span class="line">	<span class="comment">// fmt.Println(slice2[6]) // 虽然创建的切片对应底层数组的大小为 10，但是不能访问索引值 5 以后的元素,其实相当于底层数组长度是10但是切片只覆盖到了0~5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>需要说明的是，切片对应的底层数组的大小为指定的容量。比如对于上面的例子，指定了 <code>slice2</code> 的容量为 10，那么 <code>slice2</code> 对应的底层数组的大小就是 10。虽然创建的切片对应底层数组的大小为 10，但是不能访问索引值 5 以后的元素，比如：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fmt.Println(slice2[<span class="number">6</span>])</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">panic: runtime error: index out of range [6] with length 5</span><br></pre></td></tr></table></figure>
<p>虽然创建的切片对应底层数组的大小为 10，但是不能访问索引值 5 以后的元素，其实相当于：底层数组长度是10但是切片 <code>slice2</code> 只覆盖到了 <code>0~5</code>。</p>
<h1 id="切片的长度和容量">切片的长度和容量</h1>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arr := [...]<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;</span><br></pre></td></tr></table></figure>
<p>示例一：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">s1 := arr[<span class="number">3</span>:] <span class="comment">// [4 5 6 7 8 9]</span></span><br><span class="line">fmt.Println(s1)</span><br><span class="line"><span class="comment">// 切片的长度是元素的个数,切片的容量是底层数组从切片的第一个元素到最后一个元素,</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;len(s1):%d cap(s1):%d \n&quot;</span>, <span class="built_in">len</span>(s1), <span class="built_in">cap</span>(s1))</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[4 5 6 7 8 9]</span><br><span class="line">len(s1):6 cap(s1):6 </span><br></pre></td></tr></table></figure>
<p>示例二：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">s2 := arr[<span class="number">4</span>:<span class="number">8</span>] <span class="comment">// [5 6 7 8]</span></span><br><span class="line">fmt.Println(s2)</span><br><span class="line"><span class="comment">// 切片的长度是元素的个数,所以len=4,切片的容量是底层数组从切片的第一个元素到最后一个元素,所以这里就是从4到9</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;len(s2):%d cap(s2):%d \n&quot;</span>, <span class="built_in">len</span>(s2), <span class="built_in">cap</span>(s2))</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[5 6 7 8]</span><br><span class="line">len(s2):4 cap(s2):5 </span><br></pre></td></tr></table></figure>
<h1 id="切片的本质">切片的本质</h1>
<p>切片的本质就是对底层数组的封装，它包含了三个信息：底层数组的指针、切片的长度 <code>len</code> 和切片的容量 <code>cap</code>。参考自：  <a target="_blank" rel="noopener" href="https://www.liwenzhou.com/posts/Go/06_slice/">李文周的博客</a></p>
<p>举个例子，现在有一个数组<code>a := [8]int&#123;0, 1, 2, 3, 4, 5, 6, 7&#125;</code>，切片<code>s1 := a[:5]</code>，示意图如下：</p>
<p><img src="https://www.liwenzhou.com/images/Go/slice/slice_01.png" alt=""></p>
<p>切片<code>s2 := a[3:6]</code>，示意图如下：</p>
<p><img src="https://www.liwenzhou.com/images/Go/slice/slice_02.png" alt=""></p>
<p><strong>注意</strong>：现在两个切片共享同一个底层数组，因为切片的本质就是对底层数组的封装，所以如果一个切片修改了该底层数组的共享部分，另一个切片也能感知到</p>
<h1 id="切片判断是否为空">切片判断是否为空</h1>
<p>切片之间是不能比较的，不能使用<code>==</code>操作符来判断两个切片是否含有全部相等元素。 切片唯一合法的比较操作是和 <code>nil</code> 比较。 一个 <code>nil</code> 值的切片并没有底层数组，一个 <code>nil</code> 值的切片的长度和容量都是0。但是不能说一个长度和容量都是0的切片一定是 <code>nil</code> ，例如下面的示例：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">compareSlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> q1 []<span class="type">int</span> <span class="comment">// len(q1)=0;cap(q1)=0;q1==nil; 没有被初始化所以q1==nil is true</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(q1):%d cap(q1):%d q1==nil:%t \n&quot;</span>, <span class="built_in">len</span>(q1), <span class="built_in">cap</span>(q1), q1 == <span class="literal">nil</span>)</span><br><span class="line"></span><br><span class="line">	q2 := []<span class="type">int</span>&#123;&#125; <span class="comment">// len(q2)=0;cap(q2)=0;q2!=nil; 这里是定义了元素为空的数组,所以q2==nil is false</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(q2):%d cap(q2):%d q2==nil:%t \n&quot;</span>, <span class="built_in">len</span>(q2), <span class="built_in">cap</span>(q2), q2 == <span class="literal">nil</span>)</span><br><span class="line"></span><br><span class="line">	q3 := <span class="built_in">make</span>([]<span class="type">int</span>, <span class="number">0</span>) <span class="comment">// len(q3)=0;cap(q3)=0; q3!=nil; 这里使用了make所以十分分配内存的只不过cap和len都为0而已,所以q3==nil is false</span></span><br><span class="line">	fmt.Printf(<span class="string">&quot;len(q3):%d cap(q3):%d q3==nil:%t \n&quot;</span>, <span class="built_in">len</span>(q3), <span class="built_in">cap</span>(q3), q3 == <span class="literal">nil</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>所以要判断一个切片是否是空的，要是用<code>len(s) == 0</code>来判断，不应该使用<code>s == nil</code>来判断。</p>
<h1 id="切片的赋值拷贝">切片的赋值拷贝</h1>
<p>下面代码演示了拷贝前后两个变量共享底层数组，之前也说过：对一个切片的修改会影响另一个切片的内容，这点需要特别注意。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">shareArraySlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	w1 := <span class="built_in">make</span>([]<span class="type">int</span>, <span class="number">3</span>) <span class="comment">// [0 0 0]</span></span><br><span class="line">	w2 := w1             <span class="comment">// 将w1直接赋值给w2，w1和w2共用一个底层数组</span></span><br><span class="line">	w2[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">	fmt.Println(w1) <span class="comment">// [100 0 0]</span></span><br><span class="line">	fmt.Println(w2) <span class="comment">// [100 0 0]</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="切片遍历">切片遍历</h1>
<p>切片的遍历方式和数组是一致的，支持索引遍历和<code>for range</code>遍历。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">traversalSlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	slice := <span class="built_in">make</span>([]<span class="type">int</span>, <span class="number">3</span>) <span class="comment">// [0 0 0]</span></span><br><span class="line">	<span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(slice); i++ &#123;</span><br><span class="line">		fmt.Println(i, slice[i])</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> index, value := <span class="keyword">range</span> slice &#123;</span><br><span class="line">		fmt.Println(index, value)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="append">append</h1>
<p>Go语言的内建函数 <code>append()</code> 可以为切片动态添加元素。 可以一次添加一个元素，可以添加多个元素，也可以添加另一个切片中的元素（后面加<code>…</code>）。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">appendSlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	slice := <span class="built_in">make</span>([]<span class="type">int</span>, <span class="number">3</span>)              <span class="comment">// 创建切片：[0 0 0]</span></span><br><span class="line">	slice = <span class="built_in">append</span>(slice, <span class="number">1</span>)             <span class="comment">// 切片中添加第一个元素 1</span></span><br><span class="line">	slice = <span class="built_in">append</span>(slice, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>) <span class="comment">// 继续添加元素 2,3,4,5,6</span></span><br><span class="line">	slice2 := []<span class="type">int</span>&#123;<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;             <span class="comment">// 创建新的切片</span></span><br><span class="line">	slice = <span class="built_in">append</span>(slice, slice2...)     <span class="comment">// 将新的切片中的元素都放到w3中,这里...代表将slice2中的元素拆分</span></span><br><span class="line">	fmt.Println(slice)                   <span class="comment">// 输出：[0 0 0 1 2 3 4 5 6 7 8 9]</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>**注意：**如果使用 <code>append()</code> 切片可以不被初始化,会自动扩容并添加元素。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> s []<span class="type">int</span></span><br><span class="line">s = <span class="built_in">append</span>(s, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</span><br></pre></td></tr></table></figure>
<p>每个切片会指向一个底层数组，这个数组的容量够用就添加新增元素。当底层数组不能容纳新增的元素时，切片就会自动按照一定的策略进行“扩容”，此时该切片指向的底层数组就会更换。“扩容”操作往往发生在 <code>append()</code> 函数调用时，所以我们通常都需要用原变量接收append函数的返回值。</p>
<p>例如：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">appendDilatationSlice</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> numSlice []<span class="type">int</span></span><br><span class="line">	<span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">10</span>; i++ &#123;</span><br><span class="line">		numSlice = <span class="built_in">append</span>(numSlice, i)</span><br><span class="line">		fmt.Printf(<span class="string">&quot;%v  len:%d  cap:%d  ptr:%p\n&quot;</span>, numSlice, <span class="built_in">len</span>(numSlice), <span class="built_in">cap</span>(numSlice), numSlice)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[0]  len:1  <span class="built_in">cap</span>:1  ptr:0xc000012088</span><br><span class="line">[0 1]  len:2  <span class="built_in">cap</span>:2  ptr:0xc0000120d0        </span><br><span class="line">[0 1 2]  len:3  <span class="built_in">cap</span>:4  ptr:0xc000010200      </span><br><span class="line">[0 1 2 3]  len:4  <span class="built_in">cap</span>:4  ptr:0xc000010200    </span><br><span class="line">[0 1 2 3 4]  len:5  <span class="built_in">cap</span>:8  ptr:0xc00000c340  </span><br><span class="line">[0 1 2 3 4 5]  len:6  <span class="built_in">cap</span>:8  ptr:0xc00000c340</span><br><span class="line">[0 1 2 3 4 5 6]  len:7  <span class="built_in">cap</span>:8  ptr:0xc00000c340</span><br><span class="line">[0 1 2 3 4 5 6 7]  len:8  <span class="built_in">cap</span>:8  ptr:0xc00000c340</span><br><span class="line">[0 1 2 3 4 5 6 7 8]  len:9  <span class="built_in">cap</span>:16  ptr:0xc00010e080</span><br><span class="line">[0 1 2 3 4 5 6 7 8 9]  len:10  <span class="built_in">cap</span>:16  ptr:0xc00010e080</span><br></pre></td></tr></table></figure>
<p>从上面的结果可以看出：</p>
<ol>
<li><code>append()</code>函数将元素追加到切片的最后并返回该切片</li>
<li>切片 <code>numSlice</code> 的容量按照1，2，4，8，16这样的规则自动进行扩容，每次扩容后都是扩容前的2倍</li>
</ol>
<h1 id="切片的扩容策略">切片的扩容策略</h1>
<p>可以通过查看 <code>$GOROOT/src/runtime/slice.go</code> 源码，其中扩容相关代码如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">newcap := old.<span class="built_in">cap</span></span><br><span class="line">doublecap := newcap + newcap</span><br><span class="line"><span class="comment">// 1.首先判断如果新申请容量(cap)大于2倍的旧容量(old.cap):那么最终容量等于新申请的容量(cap)</span></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">cap</span> &gt; doublecap &#123;</span><br><span class="line">	newcap = <span class="built_in">cap</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 2.否则判断如果旧切片的长度小于1024，则最终容量等于旧容量(old.cap)x2</span></span><br><span class="line">	<span class="keyword">if</span> old.<span class="built_in">len</span> &lt; <span class="number">1024</span> &#123;</span><br><span class="line">		newcap = doublecap</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 3. 否则判断如果旧切片长度大于等于1024 ，则最终容量从旧容量（old.cap）开始循环增加原来的1/4，即 newcap=old.cap,for &#123;newcap += newcap/4&#125; 直到最终容量大于等于新申请的容量cap，即newcap &gt;= cap</span></span><br><span class="line">        </span><br><span class="line">		<span class="comment">// Check 0 &lt; newcap to detect overflow</span></span><br><span class="line">		<span class="comment">// and prevent an infinite loop.</span></span><br><span class="line">		<span class="keyword">for</span> <span class="number">0</span> &lt; newcap &amp;&amp; newcap &lt; <span class="built_in">cap</span> &#123;</span><br><span class="line">			newcap += newcap / <span class="number">4</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// Set newcap to the requested cap when</span></span><br><span class="line">		<span class="comment">// the newcap calculation overflowed.</span></span><br><span class="line">		<span class="keyword">if</span> newcap &lt;= <span class="number">0</span> &#123;</span><br><span class="line">			newcap = <span class="built_in">cap</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>从上面的代码可以看出以下内容：</p>
<ol>
<li>
<p>首先判断如果新申请容量  <code>cap</code> 大于2倍的旧容量  <code>old.cap</code>，最终容量  <code>newcap</code> 等于新申请的容量  <code>cap</code></p>
</li>
<li>
<p>否则判断如果旧切片的长度小于 <code>1024</code> ，则最终容量 <code>newcap </code> 等于旧容量  <code>old.cap</code> 的两倍</p>
</li>
<li>
<p>否则判断如果旧切片长度大于等于 <code>1024</code> ，则最终容量 <code>newcap</code> 从旧容量 <code>old.cap</code> 开始循环增加原来的<code>1/4</code>，即 <code>newcap=old.cap,for &#123;newcap += newcap/4&#125; </code>直到最终容量 <code>newcap</code> 大于等于新申请的容量<code>cap</code>，即 <code>newcap &gt;= cap</code></p>
</li>
<li>
<p>如果最终容量  <code>cap</code> 计算值溢出，则最终容量 <code>cap</code>  就是新申请容量 <code>cap</code></p>
</li>
</ol>
<p>需要注意：切片扩容会根据切片中元素的类型不同而做不同的处理，比如 <code>int</code> 和 <code>string</code> 类型的处理方式就不一样。</p>
<h1 id="copy">copy</h1>
<p>首先来看一个问题：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a := []<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">b := a</span><br><span class="line">fmt.Println(a) <span class="comment">// [1 2 3 4 5]</span></span><br><span class="line">fmt.Println(b) <span class="comment">// [1 2 3 4 5]</span></span><br><span class="line">b[<span class="number">0</span>] = <span class="number">1000</span></span><br><span class="line">fmt.Println(a) <span class="comment">// [1000 2 3 4 5]</span></span><br><span class="line">fmt.Println(b) <span class="comment">// [1000 2 3 4 5]</span></span><br></pre></td></tr></table></figure>
<p>由于切片是引用类型，所以 <code>a</code> 和 <code>b</code> 其实都指向了同一块内存地址。修改 <code>b</code> 的同时 <code>a</code> 的值也会发生变化。</p>
<p><code>copy()</code>函数可以迅速地将一个切片的数据复制到另外一个切片空间中，使用格式如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">copy</span>(destSlice, srcSlice []T)</span><br></pre></td></tr></table></figure>
<p>其中：</p>
<ul>
<li><code>srcSlice</code>: 数据来源切片</li>
<li><code>destSlice</code>: 目标切片</li>
</ul>
<p>举个例子：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">c := []<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">d := <span class="built_in">make</span>([]<span class="type">int</span>, <span class="number">5</span>)</span><br><span class="line"><span class="built_in">copy</span>(d, c)     <span class="comment">// 使用copy()函数将切片c1中的元素复制到切片c2</span></span><br><span class="line">fmt.Println(c) <span class="comment">// [1 2 3 4 5]</span></span><br><span class="line">fmt.Println(d) <span class="comment">// [1 2 3 4 5]</span></span><br><span class="line">c[<span class="number">0</span>] = <span class="number">1000</span></span><br><span class="line">fmt.Println(c) <span class="comment">// [1000 2 3 4 5]</span></span><br><span class="line">fmt.Println(d) <span class="comment">// [1 2 3 4 5]</span></span><br></pre></td></tr></table></figure>
<h1 id="从切片中删除元素">从切片中删除元素</h1>
<p>Go语言中并没有删除切片元素的专用方法，我们可以使用切片本身的特性来删除元素。 代码如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 从切片中删除元素</span></span><br><span class="line">c3 := []<span class="type">int</span>&#123;<span class="number">30</span>, <span class="number">31</span>, <span class="number">32</span>, <span class="number">33</span>, <span class="number">34</span>, <span class="number">35</span>, <span class="number">36</span>, <span class="number">37</span>&#125;</span><br><span class="line"><span class="comment">// 要删除索引为2的元素32</span></span><br><span class="line">c3 = <span class="built_in">append</span>(c3[:<span class="number">2</span>], c3[<span class="number">3</span>:]...) <span class="comment">// 其实这就是利用append的特性修改了切片内容再返回</span></span><br><span class="line">fmt.Println(c3)                <span class="comment">// [30 31 33 34 35 36 37]</span></span><br></pre></td></tr></table></figure>
<p>总结：要从切片c3中删除索引为 <code>index</code> 的元素，操作方法是 <code>c3 = append(c3[:index], c3[index+1:]...)</code></p>

</div>


  <div class="book-comments">
    




  </div>



<script src="/js/book-post.js"></script>

        </div>
      </div>
      <div class="column col-2 hide-lg">
        <div class="book-post-info">
  
    <div class="book-post-meta">

  <div class="author">

    <!-- Author image -->
    <div class="author-img">
      
        <figure
          class="avatar avatar-lg"
          data-initial="W"
          style="background-color: #3b4351;">
        </figure>
      
    </div>

    <!-- Author title -->
    <div class="author-title">
      <div>WangPengLiang</div>
      <div>2022-04-06</div>
    </div>
  </div>

  

  <div class="divider"></div>
</div>
  

  <div class="book-tocbot">
</div>
<div class="book-tocbot-menu">
  <a class="book-toc-expand" onclick="expand_toc()">Expand all</a>
  <a onclick="go_top()">Back to top</a>
  <a onclick="go_bottom()">Go to bottom</a>
</div>


<script src="/js/book-toc.js"></script>

</div>
      </div>
    </div>
  </div>
  
  <a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>

</body>
</html>


<script src="/js/book.js"></script>
